package thread;

class MyBlockingQueue {
    private String[] elems = null;
    //[head，tail)
    //head位置指向的是第一个元素，tail指向的是最后一个元素的下一个元素
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    public MyBlockingQueue(int capacity) {
        elems = new String[capacity];
    }

    void put(String elem) {
        if(size >= elems.length) {
            //队列满了，后续再处理阻塞的情况
            return;
        }
        //把新的元素放到tail所在位置上
        elems[tail] = elem;
        tail++;
        if(tail >= elems.length) {
            //到达末尾，就回到开头
            tail = 0;
        }
        //更新size信息
        size++;
    }

    String take() {
        if(size == 0) {
            //队列为空，后续再处理阻塞情况
            return null;
        }
        //取出head指向的元素
        String result = elems[head];
        /*head++;
        if(head >= elems.length) {
            head = 0;
        }*/
        head = (head + 1) % elems.length;
        size--;
        return result;
    }
}

public class Demo31 {
    public static void main(String[] args) {

    }
}
